﻿using SixLabors.Fonts;

namespace MicroCloud.Utils
{
    /// <summary>
    /// 字体帮助类
    /// </summary>
    public sealed class FontsHandler
    {
        /// <summary>
        /// 程序集内字体资源目录
        /// </summary>
        private const string FontsAssemblyDirectory = @"MicroCloud.Fonts";

        /// <summary>  
        /// 无参私有构造函数  
        /// </summary>  
        private FontsHandler() { }

        #region 属性
        /// <summary>
        /// 获取 字体集
        /// </summary>
        public static Dictionary<FontType, FontFamily> FontFamilys { get; } = new();
        
        /// <summary>
        /// 获取 字体文件流集合
        /// </summary>
        private static Dictionary<FontType, Stream> FontStreams { get; } = new();

        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            FontFamilys.Clear();
            FontStreams.Clear();

            FontCollection fonts = new();

            var fontTypes = FontType.Comfortaa_bold.ToList();
            foreach (var fontItem in fontTypes)
            {
                var fontType = (FontType)fontItem.Value;
                var fontStream = GetFontFile(fontType, true);
                if (!FontStreams.ContainsKey(fontType))
                {
                    FontStreams.Add(fontType, fontStream);
                }
                if (!FontFamilys.ContainsKey(fontType))
                {
                    FontFamily fontfamily = fonts.Add(fontStream); //字体的路径，升级后方法
                    FontFamilys.Add(fontType, fontfamily);
                }
            }
        }

        /// <summary>
        /// 获取字体文件流
        /// </summary>
        /// <param name="fontType">字体类型</param>
        /// <param name="copyToDirectory">复制到目录</param>
        /// <returns>文件流</returns>
        public static Stream GetFontFile(FontType fontType , bool copyToDirectory = false)
        {
            Stream fontFileStream;
            if (FontStreams.ContainsKey(fontType))
            {
                fontFileStream = FontStreams[fontType];
            }
            else
            {
                fontFileStream = GetFontFileByAssembly(fontType);
            }
            if (copyToDirectory)
            {
                GetFontFileInfo(fontType, out string fontFileName, out string fontFileExt);
                var fontFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PathConstants.FontsPath);
                var fontFileFullPath = Path.Combine(fontFilePath, $"{fontFileName}.{fontFileExt}");
                if (!Directory.Exists(fontFilePath))
                {
                    Directory.CreateDirectory(fontFilePath);
                }
                if (!File.Exists(fontFileFullPath))
                {
                    using var fileStream = File.Create(fontFileFullPath);
                    fontFileStream.Seek(0, SeekOrigin.Begin);
                    fontFileStream.CopyToAsync(fileStream);
                }
            }
            return fontFileStream;
        }

        #endregion

        #region 私有方法
        /// <summary>
        /// 从程序集中获取字体文件流
        /// </summary>
        /// <param name="fontType">字体类型</param>
        /// <returns>文件流</returns>
        private static Stream GetFontFileByAssembly(FontType fontType)
        {
            Stream fontFileStream = null;
            GetFontFileInfo(fontType, out string fontFileName, out string fontFileExt);
            if (!fontFileName.IsMissing())
            {
                var fontFileAssemblyPath = $"{FontsAssemblyDirectory}.{fontFileName}.{fontFileExt}";
                fontFileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fontFileAssemblyPath);
                if (!FontStreams.ContainsKey(fontType))
                {
                    FontStreams.Add(fontType, fontFileStream);
                }
            }
            return fontFileStream;
        }

        /// <summary>
        /// 获取字体文件信息
        /// </summary>
        /// <param name="fontType">字体类型</param>
        /// <param name="fontFileName">字体文件名称</param>
        /// <param name="fontFileExt">字体文件扩展名称</param>
        /// <returns>文件流</returns>
        private static void GetFontFileInfo(FontType fontType, out string fontFileName, out string fontFileExt)
        {
            fontFileName = "";
            fontFileExt = "ttf";
            switch (fontType)
            {
                case FontType.Comfortaa_bold:
                    fontFileName = "Comfortaa_bold";
                    break;
                case FontType.Denkiya_san:
                    fontFileName = "Denkiya_san";
                    break;
                case FontType.Dingtalk_san:
                    fontFileName = "Dingtalk_san";
                    break;
            }
        }

        #endregion
    }

}